import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_rider/app.dart';
import 'package:flutter_rider/kit/loc_kit.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import '../../api/api.dart';
import '../../generated/r.dart';
import '../../utils/screen_fit.dart';
import '../../module/login/page/login_page.dart';
import 'main_page.dart';

class SplashPage extends ConsumerStatefulWidget {
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends ConsumerState<SplashPage> {
  late Timer _timer;

  @override
  void initState() {
    super.initState();
    Api.init(ref).then((value) {
      _timer = Timer(const Duration(seconds: 2), () {
        Navigator.pushAndRemoveUntil(context,
            MaterialPageRoute(builder: (context) {
          return Api.isLogin() ? MainPage() : LoginPage();
        }), (route) => false);
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    _timer.cancel();
  }

  @override
  Widget build(BuildContext context) {
    ScreenFit.initialize(context);
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: Column(
            children: [
              const Spacer(),
              SvgPicture.asset(R.image_logo1, width: 120.rpx, height: 120.rpx),
              UText.get("智行助手", Colors.black, 20.rpx, fw: FontWeight.bold),
              80.hspace,
              const Spacer(),
            ],
          ),
        ),
      ),
    );
  }
}
